//============================================================================
// Name        : ircc.cpp
// Author      : Fran
// Version     :
// Copyright   : If you wanna copy this project, please wait until is finished
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "comandos.h"
#include "constantes.h"
#include "consola.h"
#include "variablesGlobales.h"

int print_usage(char *program) {

	fprintf(stdout, "Usage: %s [-s host:port] [-n nick] [-c channel] [-d]\n",
			program);
	fprintf(stdout, "  %s\t<host:port>\n", OPTION_HOST_PORT);
	fprintf(stdout,
			"      Automatically connects to the IRC server on <host:port>.\n");
	fprintf(stdout, "  %s\t<nick>\n", OPTION_NICK);
	fprintf(stdout,
			"      Sets  the  nickname  to  <nick>.	 The  rest  of	the parameters\n");
	fprintf(stdout,
			"      required for registration (username, realname) are also  set  to\n");
	fprintf(stdout, "      this value.\n");
	fprintf(stdout, "  %s\t<channel>\n", OPTION_CHANNEL);
	fprintf(stdout, "      Automatically joins <channel>.\n");
	fprintf(
			stdout,
			"  %s\tDebug  mode.   Developers  use this option to show debug traces,\n",
			OPTION_DEBBUG);
	fprintf(stdout, "      that a normal user MUST NOT see.\n");

	return 0;
}

int init(int argc, char *argv[]) {
	int i;
	char *ptrSeparador;
	bool arg_servidor = false, arg_nick = false, arg_canal = false;

	/*Procesa los argumentos de entrada*/
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], OPTION_DEBBUG) == 0) {
			debugMode = true;
		} else if (strcmp(argv[i], OPTION_NICK) == 0) {
			i++;
			if (i < argc) {
				strcpy(nick, argv[i]);
				strcpy(username, argv[i]);
				strcpy(realname, argv[i]);
				fprintf(stderr, "nick|%s|, username|%s|, realname|%s|\n", nick,
						username, realname);
				arg_nick = true;
			} else {
				print_usage(argv[0]);
				return -1;
			}
		} else if (strcmp(argv[i], OPTION_HOST_PORT) == 0) {
			i++;
			if (i < argc) {
				ptrSeparador = strchr(argv[i], ':');
				if (ptrSeparador) {
					*ptrSeparador = '\0';
					strcpy(servidor, argv[i]);
					if ((puerto = atoi(ptrSeparador + 1)) == 0) {
						print_usage(argv[0]);
						return -1;
					}
				} else {
					print_usage(argv[0]);
					return -1;
				}
				fprintf(stderr, "server|%s|, port|%d|\n", servidor, puerto);
				arg_servidor = true;
			} else {
				print_usage(argv[0]);
				return -1;
			}
		} else if (strcmp(argv[i], OPTION_CHANNEL) == 0) {
			i++;
			if (i < argc) {
				strncat(channelDesired, argv[i], strlen(argv[i]));
				fprintf(stderr, "channel|%s|\n", channelDesired);
				arg_canal = true;
			} else {
				print_usage(argv[0]);
				return -1;
			}
		} else {
			print_usage(argv[0]);
			return -1;
		}

	}

	/*Inicia la consola de texto*/
	if (init_consola() < 0) {
		fprintf(stderr, "ERROR init_consola\n");
		return -1;
	}

	/*Ejecuta los argumentos de entrada, si los hay*/
	if (arg_servidor) {
		if (connect(servidor, puerto) >= 0) {
			if (arg_nick) {
				if (auth(nick) >= 0) {
					if (arg_canal) {
						join(channelDesired);
					}
				}
			}
		}
	}

	//fprintf(stdout, "Hello World! :)\n");

	return 0;
}

int main(int argc, char *argv[]) {

	if (init(argc, argv) < 0) {
		return -1;
	}

	/*Inicia el bucle de la consola*/
	bucle_consola();

	return 0;
}
